#include<bits/stdc++.h>
using namespace std;
template<typename Tp> inline void read(Tp &x){
    x=0;bool f=0;
    char c=getchar();
    while(!isdigit(c)&&c!='-')c=getchar();
    if(c=='-')f=1,c=getchar();
    while(isdigit(c))x=x*10+c-'0',c=getchar();
    if(f)x=-x;
}
template<typename Tp> inline void write(Tp x){
    if(x<0)putchar('-'),x=-x;
    if(x/10)write(x/10);
    putchar(x%10+'0');
}
const int N=200005;
int T,n,A[N],dp[N][2],r[N],ans=0;
void dfs(int u,int f,int fa,int fb,int mx){
    if(u==n){
        if(f==0&&A[u]==fa)mx+=A[u];
        if(f==1&&A[u]==fb)mx+=A[u];
        ans=max(ans,mx);
        return;
    }
    if(f==0&&A[u]==fa)mx+=A[u];
    if(f==1&&A[u]==fb)mx+=A[u];
    if(f==0)fa=A[u];
    else fb=A[u];
    dfs(u+1,0,fa,fb,mx);
    dfs(u+1,1,fa,fb,mx);
}
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    read(T);
    while(T--){
        read(n);
        ans=0;
        for(int i=1;i<=n;i++){
            read(A[i]);
            if(A[i]==A[i-1])r[i]=r[i-1]+A[i];
            else r[i]=r[i-1];
        }
        if(n<=20){
            dfs(1,0,0,0,0);
        }
        else{
            vector<int> p[100005];
            for(int i=1;i<=n;i++){
                if(p[A[i]].empty())dp[i][0]=dp[i][1]=0;
                else{
                    for(int j:p[A[i]]){
                        dp[i][0]=max(dp[i][0],dp[j][0]+r[i]-r[j]+A[i]);
                        dp[i][1]=max(dp[i][1],dp[j][1]+r[i]-r[j]+A[i]);
                    }
                }
                p[A[i]].push_back(i);
            }
            ans=max(dp[n][0],dp[n][1]);
        }
        write(ans);
        putchar('\n');
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}